package com.example.demo.Controller;

import com.example.demo.Entity.*;
import com.example.demo.Service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
public class UpdateController {

    @Autowired
    private ResumeService resumeService;

    // 显示修改页面
    @GetMapping("/update/{id}")
    public String getResumePage(@PathVariable("id") Long id, Model model) {
        model.addAttribute("user", resumeService.getUser(id));
        model.addAttribute("educations", resumeService.getEducations(id));
        model.addAttribute("workExperiences", resumeService.getWorkExperiences(id));
        model.addAttribute("campusExperiences", resumeService.getCampusExperiences(id));
        model.addAttribute("skills", resumeService.getSkills(id));
        model.addAttribute("evaluation", resumeService.getEvaluation(id));
        return "update";
    }

    // 处理简历更新
    @PostMapping("/updateResume/{id}")
    public String updateResume(
            @PathVariable("id") Long userId,
            @ModelAttribute("user") PersonalInfo user,
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "educations", required = false) List<Education> educations,
            @RequestParam(value = "workExperiences", required = false) List<WorkExperience> workExperiences,
            @RequestParam(value = "campusExperiences", required = false) List<CampusExperience> campusExperiences,
            @RequestParam(value = "skills", required = false) List<Skill> skills,
            @RequestParam("evaluation.content") String evaluationContent,
            RedirectAttributes redirectAttributes) {


        // 处理逻辑...

        System.out.println(educations);
        resumeService.updateEducations(userId, educations);
        try {
//             处理头像上传
            if (!file.isEmpty()) {
                String avatarPath = saveAvatar(file, userId);
                resumeService.updateavatar(avatarPath, userId);
            }

//             更新用户基本信息
            resumeService.updateUser(user, userId);

            // 更新教育背景


            // 更新工作经历
//            resumeService.updateWorkExperiences(userId, workExperiences);

//            // 更新校园经历
//            resumeService.updateCampusExperiences(userId, campusExperiences);
//
//            // 更新技能证书
//            resumeService.updateSkills(userId, skills);
//
//            // 更新自我评价
//            Evaluation evaluation = new Evaluation();
//            evaluation.setPerson_id(userId);
//            evaluation.setContent(evaluationContent);
//            resumeService.updateEvaluation(evaluation);

            redirectAttributes.addFlashAttribute("message", "简历更新成功！");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "简历更新失败: " + e.getMessage());
            return "redirect:/update/" + userId;
        }

        return "redirect:/resume/" + userId;
    }

    // 头像保存方法
    private String saveAvatar(MultipartFile file, Long userId) throws IOException {
        // 确保上传目录存在
        String uploadDir = "src/main/resources/static/images/";
        Files.createDirectories(Paths.get(uploadDir));

        // 生成唯一文件名
        String originalFilename = file.getOriginalFilename();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFilename = "avatar_" + userId + fileExtension;

        // 保存文件
        Path path = Paths.get(uploadDir + newFilename);
        Files.write(path, file.getBytes());

        return newFilename;
    }
}